package com.wf.smartcomm.server.admin.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.aspectj.util.FileUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.MerchantUser;
import com.wf.smartcomm.server.bean.Terminal;
import com.wf.smartcomm.server.bean.UnitMachine;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.bean.Version;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.PushMessage;
import com.wf.smartcomm.server.push.Pusher;
import com.wf.smartcomm.server.response.ResponseCode;
import com.wf.smartcomm.server.service.MerchantUserService;
import com.wf.smartcomm.server.service.TerminalService;
import com.wf.smartcomm.server.service.UnitMachService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.service.VersionService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.PushUtils;

@Controller
@Scope("prototype")
public class VersionAction extends BaseAction implements ModelDriven<Version> {

	private Version version = new Version();
	
	@Resource
	private VersionService versionServiceImpl;
	@Resource
	private UnitMachService umServiceImpl;
	@Resource
	private UserService userService;
	
	@Resource
	private TerminalService terminalService;
	
	
	private List<File> upload;  
    //多个上传文件的类型集合  
    private List<String> uploadContentType;  
   //多个上传文件的文件名集合  
    private List<String> uploadFileName;
	
	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void updateUnit() throws Exception{
		
		if (upload == null) {
			printJsonData(203);
			return;
		}
		String vid = request.getParameter("vid");
		String type = request.getParameter("type");
		Integer screenSize = Integer.parseInt(request.getParameter("screenSize"));
		List<UnitMachine> unitMachines = null;
		File apk = upload.get(0);
		String fileName = uploadFileName.get(0);
		String path = request.getServletContext().getRealPath("apks");
		String fullName = path+File.separator+fileName;
		String url = request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf("/"))+"/apks/";
		version.setUrl(url+fileName);
		version.setFileName(fileName);
		version.setType(ConstantUtils.APK_TYPE_UM);
		if (!vid.equals("0")) {
			Village village = new Village(vid);
			version.setVillage(village);
			unitMachines = umServiceImpl.listByUpdate(village, type, screenSize);
		}else {
			//umServiceImpl.listAll(page);
		}
		
		File des = new File(fullName);
		FileUtil.copyFile(apk, des);
		versionServiceImpl.save(version);
		printJsonData(200);
		Pusher pusher = PushUtils.getInstance();
		PushMessage msg = new PushMessage();
		msg.setPushType(ConstantUtils.PUSH_TYPE_VERSION_UPDATE_UNIT);
		msg.setData(version.getObjectId());
		for (UnitMachine um : unitMachines) {
			pusher.push0x20Message(um.getPushId().getBytes(), outGson.toJson(msg).getBytes());
		}
		pusher.close();
	}
	
	public void updateClient() throws Exception{
		
		if (upload == null) {
			printJsonData(203);
			return;
		}
		String vid = request.getParameter("vid");
		
		File apk = upload.get(0);
		String fileName = uploadFileName.get(0);
		String path = request.getServletContext().getRealPath("apks");
		String fullName = path+File.separator+fileName;
		String url = request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf("/"))+"/apks/";
		version.setUrl(url+fileName);
		version.setFileName(fileName);
		version.setType(ConstantUtils.APK_TYPE_CLIENT);
		if (!vid.equals("0")) {
			Village village = new Village(vid);
			version.setVillage(village);
			userService.listAllByVillage(village, page);
		}else {
			userService.listAll(page);
		}
		
		File des = new File(fullName);
		FileUtil.copyFile(apk, des);
		versionServiceImpl.save(version);
		printJsonData(200);
		PushMessage msg = new PushMessage();
		msg.setPushType(ConstantUtils.PUSH_TYPE_VERSION_UPDATE_CLIENT);
		msg.setData(version.getObjectId());
		Pusher pusher = PushUtils.getClientPusher();
		for (Object object : page.getDataList()) {
			User user = (User) object;
			pusher.push0x20Message(user.getPushId().getBytes(), outGson.toJson(msg).getBytes());
		}
		pusher.close();
	}
	
	public void updateCenter() throws Exception{
		
		if (upload == null) {
			printJsonData(203);
			return;
		}
		String vid = request.getParameter("vid");
		
		File apk = upload.get(0);
		String fileName = uploadFileName.get(0);
		String path = request.getServletContext().getRealPath("apks");
		String fullName = path+File.separator+fileName;
		String url = request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf("/"))+"/apks/";
		version.setUrl(url+fileName);
		version.setFileName(fileName);
		version.setType(ConstantUtils.APK_TYPE_NIUNIU);
		File des = new File(fullName);
		FileUtil.copyFile(apk, des);
		versionServiceImpl.save(version);
		printJsonData(200);
		
		if (!vid.equals("0")) {
			User user = userService.getCenterUser(vid);
			PushMessage msg = new PushMessage();
			msg.setPushType(ConstantUtils.PUSH_TYPE_CENTER_VERSION_UPDATE);
			msg.setData(version.getObjectId());
			Pusher pusher = PushUtils.getInstance();
			pusher.push0x20Message(user.getPushId().getBytes(), outGson.toJson(msg).getBytes());
			pusher.close();
		}else {
			List<User> users = userService.listAllCenter();
			PushMessage msg = new PushMessage();
			msg.setPushType(ConstantUtils.PUSH_TYPE_CENTER_VERSION_UPDATE);
			msg.setData(version.getObjectId());
			Pusher pusher = PushUtils.getInstance();
			for (User user : users) {
				pusher.push0x20Message(user.getPushId().getBytes(), outGson.toJson(msg).getBytes());
			}
			pusher.close();
		}
	}
	
	public void push() throws Exception{
		if (upload == null) {
			printJsonData(203);
			return;
		}
		File apk = upload.get(0);
		String fileName = uploadFileName.get(0);
		String path = request.getServletContext().getRealPath("apks");
		String fullName = path+File.separator+fileName;
		String url = request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf("/niuniu"))+"/apks/";
		version.setUrl(url+fileName);
		version.setFileName(fileName);
		version.setType(ConstantUtils.APK_TYPE_NIUNIU);
		File des = new File(fullName);
		FileUtil.copyFile(apk, des);
		versionServiceImpl.save(version);
		List<Terminal> terminals = terminalService.list();
		printJsonData(200);
		PushMessage msg = new PushMessage();
		msg.setPushType(ConstantUtils.PUSH_TYPE_NIUNIU_VERSION_UPDATE);
		msg.setData(version.getObjectId());
		Pusher pusher = PushUtils.getClientPusher();
		for (Terminal terminal : terminals) {
			pusher.push0x20Message(terminal.getPushId().getBytes(), outGson.toJson(msg).getBytes());
		}
		pusher.close();
	}

	public String pushview()throws Exception{
		return "pushview";
	}
	
	public String listview()throws Exception{
		return "listview";
	}
	
	public void getLatestVersion()throws Exception{
		Version version = versionServiceImpl.getLatestVersion(ConstantUtils.APK_TYPE_NIUNIU);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", version);
		printJsonData(new Gson().toJson(map));
	}
	
	@Override
	public Version getModel() {
		return version;
	}

}
